<?php
/**
 * Created by PhpStorm.
 * User: jacklove
 * Date: 2019/9/23
 * Time: 10:46
 */

namespace app\admin\controller;

use think\Request;
use think\Db;
use think\Response;
use app\common\model\GuideModel;
use app\admin\model\Guide as Guide;
use app\admin\model\Hotel;
use app\admin\model\Food;
use app\admin\model\Team;
use app\admin\model\User;
class Live extends Common
{
    /*
     * 入住导游列表
     */
    public function guide()
    {
        if (request()->isAjax()){
            $data = input('param.');
            $page['limit'] = $data['limit'];
            $page['page']  = (int)$data['page'];
            $this->getPageAndSize($page);
            $where = [];
            $where['is_del'] = array('eq',0);
            if (isset($data['name']) && $data['name'] != ''){
                $where['name'] = array('like','%'.$data['name'].'%');
            }
            if (isset($data['guide_tel']) && $data['guide_tel'] != ''){
                $where['guide_tel'] = array('like','%'.$data['guide_tel'].'%');
            }
            if(isset($data['is_pass']) && $data['is_pass'] != ''){
                $where['is_pass'] = array('like','%'.$data['is_pass'].'%');
            }
            $guide = new guide();
            $count = $guide->getAllGuide($where);
            $datas =  $guide->getGuideByWhere($where,$this->from,$this->size);
            ajax_return(config('code.success_show'),'请求成功',$datas,$count);
        }else{
            return view();
        }
    }
    /*
     * 入住导游申请通过不通过，后期加短信通知 0没有入住1景区，2酒店，3美食商家，4旅行社，5导游
     */
    public function guideIsPass()
    {
        if (request()->isAjax()){
            $postData = input('param.');
            $where = "guide_id = {$postData['id']}";
            $where1 = "uid = {$postData['uid']}";
            $guide = new Guide();
            $data['is_pass'] = $postData['is_pass'];
            $data['pass_time'] = time();
            $res = $guide->isPass($where,$data);
            if($res){
                $updateUs = new User();
                if($postData['is_pass']==1){
                    $updateUs->updateUserStatus($where1,5);
                }
                return 0;
            }else{
                return 1;
            }
        }
    }
    /*
     * 入住酒店列表
     */
    public function hotel()
    {
        if (request()->isAjax()){
            $data = input('param.');
            $page['limit'] = $data['limit'];
            $page['page']  = (int)$data['page'];
            $this->getPageAndSize($page);
            $where = [];
            $where['is_del'] = array('eq',0);
            if (isset($data['hotel_name']) && $data['hotel_name'] != ''){
                $where['hotel_name'] = array('like','%'.$data['hotel_name'].'%');
            }
            if (isset($data['hotel_tel']) && $data['hotel_tel'] != ''){
                $where['hotel_tel'] = array('like','%'.$data['hotel_tel'].'%');
            }
            if(isset($data['is_pass']) && $data['is_pass'] != ''){
                $where['is_pass'] = array('like','%'.$data['is_pass'].'%');
            }
              if (isset($data['app_time']) && $data['app_time'] != ''){
                $created_at=explode(' - ', $data['app_time']); 
                if (isset($created_at) && is_array($created_at)){
                    foreach($created_at as $k=>$v)
                    {
                       $created_at[$k] = strtotime($v);
                    }  
                }
              $where['app_time']=['between',$created_at]; 
            } 
            $guide = new Hotel();
            $count = $guide->getAllHotel($where);
            $datas =  $guide->getHotelByWhere($where,$this->from,$this->size);
			foreach($datas as $key=>$val)
			{
				$datas[$key]['app_time'] = date('Y-m-d H:i:s',$val['app_time']);
                if(!empty($val['examine_time'])){
                    $datas[$key]['examine_time'] = date('Y-m-d H:i:s',$val['examine_time']);
                }
               $datas[$key]['nickname'] = Db::name('user')->where("uid",$val['user_id'])->value("nickname"); 
			}
            ajax_return(config('code.success_show'),'请求成功',$datas,$count);
        }else{
            return view();
        }
    }
	/*
     * 入驻导游详情
     */
	public function guidedetail()
	{ 
        if (request()->isAjax()) {
            $postData = input('param.');
            $where = "guide_id= {$postData['guide_id']}";
            $guide = new Guide();
            $guideInfo = $guide->getGuideById($where);
            ajax_return(config('code.success_show'), '请求成功', $guideInfo);
        }
	}
    /*
    * 入住酒店申请通过不通过，后期加短信通知 0没有入住1景区，2酒店，3美食商家，4旅行社，5导游
    */
    public function hotelIsPass()
    {
        if (request()->isAjax()){
            $postData = input('param.');
            $where = "hotel_id = {$postData['id']}";
            $where1 = "uid = {$postData['uid']}";
            $hotel = new Hotel();
             if(empty($postData['reject_reasons']))
                $postData['reject_reasons'] = '';
            $data['is_pass'] = $postData['is_pass'];
            $data['examine_time'] = time();
            $data['last_settlement_date'] = date('Y-m-d',time());
            $data['reject_reasons'] = $postData['reject_reasons'];
            $res = $hotel->isPass($where,$data); 
            //生成账号和密码发送给商户
            $password = substr(base_convert(md5(uniqid(md5(microtime(true)),true)), 16, 10), 0, 6);
            $send = [
                '1'=>$postData['hotel_name'],
                '2'=>$postData['send_tel'],
                '3'=>$password,
                '5'=>$postData['hotel_name']
            ];
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $hashPassword = $hasher->HashPassword($password);
            if($res){
                $updateUs = new User();
                if($postData['is_pass']==1){
                     $up['hotel_id'] = $postData['id']; 
                     $updateUs->updateUserStatusId($where1,$up);
                     sendJsmsMessage($postData['send_tel'],176238,$send);
                   send_user_mes($postData['uid'], '商家入驻提醒',"您入驻的店铺".$postData['hotel_name']." 已通过审核，账号等信息将以短信行驶发送至您的手机，请注意查收");
                    //插入到商户后台数据库
                    $datw = [
                        'h_admin_name'=>$postData['send_tel'],
                        'admin_rename'=>$postData['hotel_name'],
                        'password'=>$hashPassword,
                        'hotel_id'=>$postData['id'],
                        'add_time'=>date('Y-m-d H:i:s')
                    ];
                    Db::name('hotel_admin')->insertGetId($datw);
                }else{
                    $refuse = [
                       '1'=>$postData['hotel_name'],
                       '2'=>$postData['reject_reasons']

                    ];
                      sendJsmsMessage($postData['send_tel'],176597,$refuse);
                      send_user_mes( $postData['uid'],"商家入驻提醒","您入驻的店铺".$postData['hotel_name']."已拒绝，拒绝原因".$postData['reject_reasons'].',您可以重新在app内申请'); 
                }
                return 0;
            }else{
                return 1;
            }
        }
    }

    /*
     * 入住美食商家列表
     */
    public function food()
    {

        if (request()->isAjax()){
            $data = input('param.');
            $page['limit'] = $data['limit'];
            $page['page']  = (int)$data['page'];
            $this->getPageAndSize($page);
            $where = [];
            $where['is_del'] = array('eq',0);
            if (isset($data['food_store_name']) && $data['food_store_name'] != ''){
                $where['food_store_name'] = array('like','%'.$data['food_store_name'].'%');
            }
            if (isset($data['food_store_tel']) && $data['food_store_tel'] != ''){
                $where['food_store_tel'] = array('like','%'.$data['food_store_tel'].'%');
            }
            if(isset($data['is_pass']) && $data['is_pass'] != ''){
                $where['is_pass'] = array('like','%'.$data['is_pass'].'%');
            }
             if (isset($data['app_time']) && $data['app_time'] != ''){
                $created_at=explode(' - ', $data['app_time']); 
                if (isset($created_at) && is_array($created_at)){
                    foreach($created_at as $k=>$v)
                    {
                       $created_at[$k] = strtotime($v);
                    }  
                }
              $where['app_time']=['between',$created_at]; 
            } 
            $guide = new Food();
            $count = $guide->getAllFoodStore($where);
            $datas =  $guide->getFoodStoreByWhere($where,$this->from,$this->size);
            if(count($datas)>0){ 
                foreach($datas as $key=>$val)
                { 
                   $datas[$key]['nickname'] = Db::name('user')->where("uid",$val['uid'])->value("nickname"); 
                }
            }
            ajax_return(config('code.success_show'),'请求成功',$datas,$count);
        }else{
            return view();
        }
    }
    /*
    * 入住酒店申请通过不通过，后期加短信通知 0没有入住1景区，2酒店，3美食商家，4旅行社，5导游
    */
    public function foodIsPass()
    {
        if (request()->isAjax()){
            $postData = input('param.');
            $where = "food_store_id = {$postData['id']}";
            $where1 = "uid = {$postData['uid']}";
            $hotel = new Food();
             if(empty($postData['reject_reasons']))
                $postData['reject_reasons'] = '';
            $data['is_pass'] = $postData['is_pass'];
            $data['examine_time'] = time();
            $data['reject_reasons'] = $postData['reject_reasons'];
            $data['last_settlement_date'] = date('Y-m-d',time());
            $send = [
                '1'=>$postData['food_store_name'],
                '2'=>$postData['food_store_name']
            ];
            $res = $hotel->isPass($where,$data);
            if($res){
                $updateUs = new User();
                if($postData['is_pass']==1){
                     $up['food_store_id'] = $postData['id']; 
                     $updateUs->updateUserStatusId($where1,$up); 
                     sendJsmsMessage($postData['send_tel'],174978,$send);
                     send_user_mes($postData['uid'], '商家入驻提醒',"您入驻的店铺".$postData['food_store_name']." 已通过审核,账号等信息将以短信行驶发送至您的手机，请注意查收");
                }else{
                     $refuse = [
                       '1'=>$postData['food_store_name'],
                       '2'=>$postData['reject_reasons']

                    ];
                     sendJsmsMessage($postData['send_tel'],176597,$refuse);
                     send_user_mes( $postData['uid'],"商家入驻提醒","您入驻的店铺".$postData['food_store_name']."已拒绝，拒绝原因".$postData['reject_reasons'].",您可以重新在app内申请"); 
                }
                return 0;
            }else{
                return 1;
            }
        }
    }
    /*
     * 入住旅行社列表
     */
    public function team()
    {
        if (request()->isAjax()){
            $data = input('param.');
            $page['limit'] = $data['limit'];
            $page['page']  = (int)$data['page'];
            $this->getPageAndSize($page);
            $where = [];
            $where['is_del'] = array('eq',0);
            if (isset($data['team_name']) && $data['team_name'] != ''){
                $where['team_name'] = array('like','%'.$data['team_name'].'%');
            }
            if (isset($data['team_phone']) && $data['team_phone'] != ''){
                $where['team_phone'] = array('like','%'.$data['team_phone'].'%');
            }
            if(isset($data['is_pass']) && $data['is_pass'] != ''){
                $where['is_pass'] = array('like','%'.$data['is_pass'].'%');
            }
             if (isset($data['app_time']) && $data['app_time'] != ''){
                $created_at=explode(' - ', $data['app_time']); 
                if (isset($created_at) && is_array($created_at)){
                    foreach($created_at as $k=>$v)
                    {
                       $created_at[$k] = strtotime($v);
                    }  
                }
              $where['app_time']=['between',$created_at]; 
            } 
            $guide = new Team();
            $count = $guide->getAllTeam($where);
            $datas = $guide->getTeamByWhere($where,$this->from,$this->size);
            if(count($datas)>0){ 
                foreach($datas as $key=>$val)
                { 
                   $datas[$key]['nickname'] = Db::name('user')->where("uid",$val['uid'])->value("nickname"); 
                }
            }
            ajax_return(config('code.success_show'),'请求成功',$datas,$count);
        }else{
            return view();
        }
    }
    /*
        * 入住酒店申请通过不通过，后期加短信通知 0没有入住1景区，2酒店，3美食商家，4旅行社，5导游
        */
    public function teamIsPass()
    {
        if (request()->isAjax()){
            $postData = input('param.');
            $where = "team_id = {$postData['id']}";
            $where1 = "uid = {$postData['uid']}";
            if(empty($postData['reject_reasons']))
                $postData['reject_reasons'] = '';
            $team = new Team();
            $data['is_pass'] = $postData['is_pass'];
            $data['examine_time'] = time();
            $data['reject_reasons'] = $postData['reject_reasons']; 
            $data['last_settlement_date'] = date('Y-m-d',time());
            //生成账号和密码发送给商户
            $password = substr(base_convert(md5(uniqid(md5(microtime(true)),true)), 16, 10), 0, 6);
            $send = [
                '1'=>$postData['team_name'],
                '2'=>$postData['send_tel'],
                '3'=>$password,
                '5'=>$postData['team_name']
            ];
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $hashPassword = $hasher->HashPassword($password);
            $res = $team->isPass($where,$data);
            if($res){
                $updateUs = new User();
                if($postData['is_pass']==1){
                     $up['team_id'] = $postData['id']; 
                     $updateUs->updateUserStatusId($where1,$up);  
                     sendJsmsMessage($postData['send_tel'],176239,$send);
                     //给用户发送消息提示 1景点订单 2酒店订单 3旅行社订单
                     send_user_mes($postData['uid'], '商家入驻提醒',"您入驻的店铺".$postData['team_name']." 已通过审核，账号等信息将以短信行驶发送至您的手机，请注意查收"); 
                    //插入到商户后台数据库
                    $datw = [
                        't_admin_tel'=>$postData['send_tel'],
                        'admin_rename'=>$postData['team_name'],
                        'password'=>$hashPassword,
                        'team_id'=>$postData['id'],
                        'team_id'=>$postData['id'],
                        'add_time'=>date('Y-m-d H:i:s')
                    ];
                    Db::name('team_admin')->insertGetId($datw);
                }else{ 
                    $refuse = [
                       '1'=>$postData['team_name'],
                       '2'=>$postData['reject_reasons']

                    ];
                    sendJsmsMessage($postData['send_tel'],176597,$refuse);
                  //给用户发送消息提示 1景点订单 2酒店订单 3旅行社订单
                 send_user_mes( $postData['uid'],"商家入驻提醒","您入驻的店铺".$postData['team_name']."已拒绝，拒绝原因".$postData['reject_reasons'].",您可以重新在app内申请"); 
                }
                return 0;
            }else{
                return 1;
            }
        }
    }
    /*
    *酒店刪除
    */
    public function hotel_del()
    {
         if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('hotel')->where('hotel_id', $post['id'])->setField('is_del', 1);
            if ($res) {
                return '1';
            } else {
                return '2';
            }
        }
    }

    /*
    *美食刪除
    */
    public function food_del()
    {
         if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('food_store')->where('food_store_id', $post['id'])->setField('is_del', 1);
            if ($res) {
                return '1';
            } else {
                return '2';
            }
        }
    }

    /*
    *景點刪除
    */
    public function team_del()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('team')->where('team_id', $post['id'])->setField('is_del', 1);
            if ($res) {
                return '1';
            } else {
                return '2';
            }
        }
    }
}